package com.lsx143.wordcount.day2;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class KeyByTest {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        DataStreamSource<Integer> dss = env.fromElements(1, 2, 3, 4, 66, 77, 88, 99);
        dss.keyBy(new KeySelector<Integer, String>() {
            @Override
            public String getKey(Integer num) {
                if (num % 2 == 0) {
                    return "偶数";
                } else {
                    return "奇数";
                }
            }
        }).process(new KeyedProcessFunction<String, Integer, Object>() {
            @Override
            public void processElement(Integer value, Context ctx, Collector<Object> out) throws Exception {

            }
        })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
